#include "GUIOverlay.h"
#include "ResourceManagers.h"
#include "Nest.h"
#include <iomanip>
#include <sstream>
#include <cstdio>
using namespace std;

GUIOverlay::GUIOverlay(void)
{
	m_hatchText.SetFont(*FontManager::Instance()->GetFont("arial.ttf"));
	m_heatText.SetFont(*FontManager::Instance()->GetFont("arial.ttf"));
	m_switchText.SetFont(*FontManager::Instance()->GetFont("arial.ttf"));
	m_hatchPlayerText.SetFont(*FontManager::Instance()->GetFont("arial.ttf"));
	m_elapsedText.SetFont(*FontManager::Instance()->GetFont("arial.ttf"));
	m_elapsedText.SetSize(20);
	//m_changeSprite= new AniSprite(*ImageManager::Instance()->GetImage("cartel_cambio.png"),512,165);
	m_changeSprite= new AniSprite(*ImageManager::Instance()->GetImage("changeS.png"),445,90);
	m_puffSprite=new Sprite(*ImageManager::Instance()->GetImage("poof.png"));
	m_puffSprite->SetCenter(m_puffSprite->GetImage()->GetWidth()/2.0f,m_puffSprite->GetImage()->GetHeight()/2.0f);
	m_puffSprite->SetPosition(400,300);
	m_puffSprite->SetScale(0.5f,0.5f);
	m_elapsedText.SetPosition(500,0);

	m_changeSprite->SetLoopSpeed(5);
	//m_changeSprite->SetPosition(400,250);
	m_changeSprite->SetPosition(400,200);


	m_hatchText.SetPosition(350,150);
	m_switchText.SetPosition(250,0);
	m_hatchPlayerText.SetPosition(500,0);

	m_heatText.SetText("Calor");
	m_heatText.SetPosition(100,15);
	wichMustHatch=0;
	temp_bar=new ProgBar(ImageManager::Instance()->GetImage("temp_bar.png"), ImageManager::Instance()->GetImage("marco_temp_bar.png"));
	change_bar=new ProgBar(ImageManager::Instance()->GetImage("bar_change.png"), ImageManager::Instance()->GetImage("marco_change.png"), sf::Vector2f(800-132, 0), 255);
	changeSound.SetBuffer(*SoundBufferManager::Instance()->GetBuffer("change.wav"));
	puffSound.SetBuffer(*SoundBufferManager::Instance()->GetBuffer("poof.ogg"));
}


GUIOverlay::~GUIOverlay(void){
    delete m_changeSprite;
    delete temp_bar;
    delete m_puffSprite;
}


void GUIOverlay::Update(float dt){
	temp_bar->Update(dt);
	if(timeToShowChange>0){
		timeToShowChange-=dt;
		m_changeSprite->Update();
	}
	if(timeToShowPuff>0)
		timeToShowPuff-=dt;




}

void GUIOverlay::ShowChangeText(float time){

	changeSound.Play();
	timeToShowChange=time;
	m_changeSprite->Play(0,2);
	//m_changeSprite->Play(0,1);


}

void GUIOverlay::ShowPuffSprite(float time){

	timeToShowPuff=time;
	puffSound.Play();

}

void GUIOverlay::Draw(RenderWindow* wnd){
		wnd->Draw(m_hatchText);
		//wnd->Draw(m_heatText);
//		wnd->Draw(m_switchText);
		wnd->Draw(m_hatchPlayerText);
		temp_bar->Draw(wnd);
		change_bar->Draw(wnd);

		

		if(timeToShowPuff>0)
			wnd->Draw(*m_puffSprite);

		wnd->Draw(m_elapsedText);

        if(wichMustHatch!=0){
            sf::Sprite s;
            sf::Image *i;
            if(wichMustHatch==1){
                i=ImageManager::Instance()->GetImage("hatch_p1.png");
            }else{
                i=ImageManager::Instance()->GetImage("hatch_p2.png");
            }
            s.SetImage(*i);
            s.SetPosition(Nest::Instance()->GetPosition());
            s.SetCenter(i->GetWidth()/2, i->GetHeight()/2);
            s.Move(0, -100);
            wnd->Draw(s);
        }
		if(timeToShowChange>0)
			wnd->Draw(*m_changeSprite);

}

GUIOverlay* GUIOverlay::Instance(){

		static GUIOverlay instance;
		return &instance;
}

void GUIOverlay::SetHatchKeys(std::string text){
	m_hatchText.SetText(text);
}

void GUIOverlay::SetHeat(std::string text){
	m_heatText.SetText(text);
}

void GUIOverlay::SetTimeToSwitch(float time){
    // actualizar la barra
    change_bar->SetValue(time);
}

void GUIOverlay::SetTotalTimeToSwitch(float time){
    change_bar->SetMaxMin(0, time);
}

void GUIOverlay::SetPlayerHatching(int i){
	wichMustHatch=i;
}




void GUIOverlay::SetElapsedTime(float value){
	char time[50];
	int min, secs, mil;
	min=((int)value)/60;
    secs=((int)value)-min*60;
    mil=(value-secs-min*60)*100;
	sprintf(time,"   Time   \n%02i:%02i:%02i",min, secs,mil);
	m_elapsedText.SetText(time);
	m_elapsedText.SetPosition(365, 0);
}


void GUIOverlay::SetHeatMaxMin(float max,float min){
	temp_bar->SetMaxMin(max,min);
}

void GUIOverlay::SetHeat(float value){
	temp_bar->SetValue(value);
}
